package org.leiyang.controller;

import org.leiyang.services.BigDataSearcherService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/big/data")
public class BigDataSearcherCtr {

    private final BigDataSearcherService bigDataSearcherService;

    public BigDataSearcherCtr(BigDataSearcherService bigDataSearcherService) {
        this.bigDataSearcherService = bigDataSearcherService;
    }

    /**
     * 计算inventory的和，price的平均值
     * @param requeseIdsFilePath
     * @return
     */
    @PostMapping("/compute/v1")
    public String computeByRequestIdFile(@RequestParam("path") String requeseIdsFilePath) {
        double[] data = bigDataSearcherService.computeByIdFile(requeseIdsFilePath);
        return "inventory的和: " + data[0] + ";price的平均值: " + data[1];
    }

    @PostMapping("/reset")
    public String resetSourceData() {
        bigDataSearcherService.resetSourceData();
        return "success";
    }

    /**
     * 计算inventory的和，price的平均值 （改进版）
     */
    @PostMapping("/compute/v2")
    public String computeByRequestIdFile_V2(@RequestParam("path") String requeseIdsFilePath) {
        double[] data = bigDataSearcherService.computeByIdFileV1(requeseIdsFilePath);
        return "inventory的和: " + data[0] + ";price的平均值: " + data[1];
    }
}